package org.usts.trainning.supermarket.service;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.usts.trainning.supermarket.dao.ProviderDao;
import org.usts.trainning.supermarket.entity.Provider;

public class ProContribute {
	ProviderDao dao = new ProviderDao();

	public String getAllBill(HttpServletRequest request, HttpServletResponse response) {
		List<Provider> list = dao.findAllProvider();
		dao.close();
		// 代码（1）保存处理结果并返回跳转页面
		request.setAttribute("listallpro", list);
		return "/providerList.jsp";
	}

	public String findProById(HttpServletRequest request, HttpServletResponse response) {
		String psupplier = request.getParameter("pid");
		Provider provider = dao.findProById(Integer.parseInt(psupplier));
		dao.close();
		// 代码（1）保存处理结果并返回跳转页面
		request.setAttribute("lookpro", provider);
		return "/providerView.jsp";
	}

	public String updateProById(HttpServletRequest request, HttpServletResponse response) {
		String psupplier = request.getParameter("pid");
		Provider provider = dao.findProById(Integer.parseInt(psupplier));
		dao.close();
		// 代码（1）保存处理结果并返回跳转页面
		request.setAttribute("updatepro", provider);
		return "/providerUpdate.jsp";
	}

	public String modifyProById(HttpServletRequest request, HttpServletResponse response) {
		Provider provider = new Provider();
		provider.setPid(Integer.parseInt(request.getParameter("pid")));
		provider.setPcode(request.getParameter("pcode"));
		provider.setPsupplier(request.getParameter("psupplier"));
		provider.setContact(request.getParameter("contact"));
		provider.setTel(request.getParameter("tel"));
		provider.setFax(request.getParameter("fax"));
		provider.setPtime(request.getParameter("ptime"));
		provider.setPdesc(request.getParameter("pdesc"));
		boolean flaf = dao.updateProById(provider);
		getAllBill(request, response);
		dao.close();
		// 代码（1）保存处理结果并返回跳转页面
		request.setAttribute("modifypro", provider);
		return "/providerList.jsp";
	}

	public String deleteProById(HttpServletRequest request, HttpServletResponse response) {
		String pid = request.getParameter("pid");
		boolean flage = dao.deletePro(Integer.parseInt(pid));
		getAllBill(request, response);
		dao.close();
		// 代码（1）保存处理结果并返回跳转页面
		return "/providerList.jsp";
	}

	public String addPro(HttpServletRequest request, HttpServletResponse response) {
		Provider provider = new Provider();
		provider.setPcode(request.getParameter("pcode"));
		provider.setPsupplier(request.getParameter("psupplier"));
		provider.setContact(request.getParameter("contact"));
		provider.setTel(request.getParameter("tel"));
		provider.setFax(request.getParameter("fax"));
		provider.setPtime(request.getParameter("ptime"));
		provider.setPdesc(request.getParameter("pdesc"));
		boolean flag = dao.addPro(provider);
		getAllBill(request, response);
		dao.close();
		// 代码（1）保存处理结果并返回跳转页面
		return "/providerList.jsp";
	}

	public String searchPro(HttpServletRequest request, HttpServletResponse response) {
		String psupplier = request.getParameter("psupplier");
		List<Provider> list = dao.findAllProvider(psupplier);
		dao.close();
		// 代码（1）保存处理结果并返回跳转页面
		request.setAttribute("listallpro", list);
		return "/providerList.jsp";
	}

}
